package test;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class UnsafeTest {
    public byte b=8;
    public int num=15;
    public char c=20;
    public short snum=40;
    public String str1="1",str2="a";

    public UnsafeTest(){}

    public static void main(String[] arg) throws Exception{
        Field f=Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe us=(Unsafe)f.get(null);

        UnsafeTest ust=new UnsafeTest();

        Class clazz=UnsafeTest.class;
        Field[] fs=clazz.getFields();
        for(Field i:fs){
            if(i.getType()==int.class)
                System.out.println(us.getInt(ust,us.objectFieldOffset(i)));
            else if(i.getType()==char.class)
                System.out.println(us.getChar(ust,us.objectFieldOffset(i)));
            else if(i.getType()==String.class)
                System.out.println(us.getObject(ust,us.objectFieldOffset(i)));
//            System.out.println(us.objectFieldOffset(i));
        }
    }
}
